const express = require('express')
const router = express.Router()
const service = require('../service/userTelService')
const Joi = require('joi')
const PATH = '/demo'

router.get('/queryById', async (req, res) => {
  const data = await service.queryById(1)
  res.json(data)
})

router.post('/helloJson', async function (req, res) {
  const schema = Joi.object({
    id: Joi.number().required().label('id'), // 不能更新
    name: Joi.string().min(11).label('name') // 不能更新
  })
  try {
    await schema.validateAsync(req.body)
    res.json(req.body)
  } catch (err) {
    console.log(err.message)
  }
})

router.post('/post/hello', function (req, res) {
  //    console.log(req.body)
  //    console.log(req.body.name)
  res.json({ ...req.body, source: 'nodejs', time: new Date() })
})

module.exports = (app) => {
  app.use(PATH, router)
}
